﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

using WebSite.Domain;
using System.Web.UI.WebControls;

namespace WebSite.DAL {
    public class RespostaDiscretaDAO{

        public static List<RespostaDiscreta> BuscarRespostaDiscreta(int idItemProntuario) 
        {
            SqlDataReader dr;
            string strsql = "Select * from tab_RespostaDiscreta where idItemProntuario = @idItemProntuario";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            List<RespostaDiscreta> respostasDiscretas;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                objCommand.Parameters.Add(new SqlParameter("@idItemProntuario", idItemProntuario));

                dr = objCommand.ExecuteReader();
                respostasDiscretas = new List<RespostaDiscreta>();
                if(dr.HasRows)
                {
                    while (dr.Read())
                    {
                        respostasDiscretas.Add(new RespostaDiscreta(Convert.ToInt32(dr["idRespostasDiscretas"]), dr["RespostaDiscreta"].ToString(),idItemProntuario));
                    }
                }
                else
                {
                    respostasDiscretas = null;
                }

                return respostasDiscretas;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();
            }            
        }

        public static string BuscarNomeRespostaDiscreta(int idRespostaDiscreta)
        {
            SqlDataReader dr;
            string strsql = "Select RespostaDiscreta from tab_RespostaDiscreta where idRespostasDiscretas = @idRespostaDiscreta";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            string respostaDiscreta = string.Empty;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                objCommand.Parameters.Add(new SqlParameter("@idRespostaDiscreta", idRespostaDiscreta));

                dr = objCommand.ExecuteReader();
                
                if (dr.HasRows)
                {
                    dr.Read();
                    respostaDiscreta = dr["RespostaDiscreta"].ToString();                                          
                }

                return respostaDiscreta;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();
            }
        }

        public static void CadastrarRespostaDiscreta(RespostaDiscreta respostaDiscreta)
        {
            string strsql = "INSERT INTO tab_RespostaDiscreta(RespostaDiscreta, idItemProntuario) VALUES (@RespostaDiscreta,(select IDENT_CURRENT('SistemaProntuario.dbo.tab_ItemProntuario')));";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand(strsql, objConnection);
            objCommand.Parameters.Add(new SqlParameter("@RespostaDiscreta", respostaDiscreta.nome));            
            objCommand.ExecuteNonQuery();
            objConnection.Close();
        }




    }
}